package com.ka.magicexplorer.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.ka.magicexplorer.activity.DocumentActivity;
import com.ka.magicexplorer.activity.GalleryActivity;
import com.ka.magicexplorer.activity.MediaActivity;
import com.ka.magicexplorer.activity.R;
import com.ka.magicexplorer.adapter.AdapterMenuLeft;
import com.ka.magicexplorer.provider.MySession;
import com.ka.magicexplorer.provider.MySession.MenuState;
import com.ka.magicexplorer.utils.Navigation;

public class FragmentMenu extends Fragment {

	protected ExpandableListView lv;
	protected AdapterMenuLeft ad;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.menu, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		init();
		execute();
	}

	private void init() {
		lv = (ExpandableListView) getActivity().findViewById(
				R.id.listview_menu_left);

		ad = new AdapterMenuLeft(getActivity());
	}

	private void execute() {
		lv.setAdapter(ad);

		switch (MySession.getInstance().getCurrentGroupFocus()) {
		case documentState:
			lv.expandGroup(0);
			break;
		case galleryState:
			lv.expandGroup(1);
			break;
		case mediaState:
			lv.expandGroup(2);
			break;
		default:
			break;
		}

		lv.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {

				int ID = (int)id;
				switch (ID) {
				case 1:
					setFocus(MenuState.documentState);
					Navigation.startActivityWithoutTransition(getActivity(), DocumentActivity.class);
					break;
				case 2:
					setFocus(MenuState.galleryState);
					Navigation.startActivityWithoutTransition(getActivity(), GalleryActivity.class);
					break;
				case 3:
					setFocus(MenuState.mediaState);
					Navigation.startActivityWithoutTransition(getActivity(), MediaActivity.class);
					break;

				default:
					break;
				}
				
				
				
				return true;
			}
		});
	}
	
	private void setFocus(MenuState state) {
		MySession.getInstance().setCurrentGroupFocus(state);
		MySession.getInstance().setCurrentChildFocus(MySession.MenuState.noState);
	}
}
